﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Security.Cryptography;

namespace DupeFilter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        string GetMD5Hash(string fileName)
        {
            byte[] file = File.ReadAllBytes(fileName);
            MD5 md5 = MD5.Create();
            byte[] bHash = md5.ComputeHash(file);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bHash.Length; i++)
            {
                sb.Append(bHash[i].ToString("X2"));
            }

            return sb.ToString();
        }

        private void btnFindDuplicates_Click(object sender, RoutedEventArgs e)
        {
            dtbSearchPath.Width = double.NaN;
            lvwEmptyFolders.Items.Clear();

            Dictionary<long, List<string>> filesBySize = new Dictionary<long, List<string>>();

            string SearchPath = dtbSearchPath.Path;
            if (SearchPath != string.Empty)
            {
                List<string> allFiles = DirectoryHelper.GetAllFiles(SearchPath);

                foreach (string s in allFiles)
                {
                    FileInfo fi = new FileInfo(s);

                    if (fi.Length > 0)
                    {
                        if (!filesBySize.ContainsKey(fi.Length))
                        {
                            filesBySize.Add(fi.Length, new List<string>());
                        }
                        filesBySize[fi.Length].Add(s);
                    }
                }

                foreach (long l in filesBySize.Keys)
                {
                    if (filesBySize[l].Count > 1)
                    {
                        foreach (string s in filesBySize[l])
                        {
                            lvwEmptyFolders.Items.Add(new FileDetails { Path = s, MD5Hash = GetMD5Hash(s) });
                        }

                        lvwEmptyFolders.Items.Add(new ListViewItem());
                    }
                }
            }
        }

        private void btnFindEmptyFolders_Click(object sender, RoutedEventArgs e)
        {
            lvwEmptyFolders.Items.Clear();

            string SearchPath = dtbSearchPath.Path;
            if (SearchPath != string.Empty)
            {
                List<string> subDirectories = DirectoryHelper.GetAllDirectories(SearchPath);

                foreach (string s in subDirectories)
                {
                    if (Directory.GetDirectories(s).Count() == 0 && Directory.GetFiles(s, "*.*").Count() == 0)
                    {
                        lvwEmptyFolders.Items.Add(s);
                    }
                }
            }
        }
    }
}
